import React, {useState} from "react";

import {PAGE_TYPE} from "../../shared/utilities/constant";
import ViewAllFault from "./ViewAllFault";
import AddFault from "./AddFault";
import ViewFault from "./ViewFault";

const FaultList = (props) => {
    const [currentPage, setCurrentPage] = useState(PAGE_TYPE.VIEW_ALL);
    const [selected, setSelected] = useState(null);

    const showViewAll = () => setCurrentPage(PAGE_TYPE.VIEW_ALL);

    const rowClickHandler = (item) => {
        if (item) {
            setSelected(item);
            setCurrentPage(PAGE_TYPE.VIEW_SINGLE);
        }
    };

    return (
        <React.Fragment>
            {(currentPage === PAGE_TYPE.VIEW_ALL) && <ViewAllFault
                onAdd={() => setCurrentPage(PAGE_TYPE.ADD_NEW)}
                onRowClick={rowClickHandler}
            />}

            {(currentPage === PAGE_TYPE.ADD_NEW) && <AddFault
                onConfirm={showViewAll}
                onClose={showViewAll}
            />}

            {(currentPage === PAGE_TYPE.VIEW_SINGLE) && <ViewFault
                data={selected}
                onClose={showViewAll}
            />}
        </React.Fragment>
    );
};

export default FaultList;
